package com.refusea.train.domain.helper

import com.refusea.train.config.mvc.WebSession
import com.refusea.train.domain.entity.LessonScheduleConfig
import com.refusea.train.domain.ex.BizEx
import com.refusea.train.mapper.LessonScheduleConfigMapper
import org.springframework.stereotype.Component

/**
 * @author Zhang Yan 2024/4/10
 **/
@Component
class LessonScheduleConfigHelper(private val lessonScheduleConfigMapper: LessonScheduleConfigMapper) {

    fun select(): LessonScheduleConfig {
        val schoolId = WebSession.user().schoolId
        return lessonScheduleConfigMapper.select(schoolId) ?: LessonScheduleConfig.default(schoolId)
    }

    fun save(lessonScheduleConfig: LessonScheduleConfig): Int {
        if (lessonScheduleConfig.id == 0) {
            lessonScheduleConfig.schoolId = WebSession.user().schoolId
            lessonScheduleConfigMapper.insert(lessonScheduleConfig)
        } else {
            val result = lessonScheduleConfigMapper.update(lessonScheduleConfig)
            if (result == 0) {
                throw BizEx.of("参数异常，请重新登录")
            }
        }
        return lessonScheduleConfig.id
    }
}
